/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ${USER}
 * Date: ${YEAR}-${MONTH}-${DAY}
 * Time: ${TIME}
 */
class Solution {
    public String countAndSay(int n) {
        if(n == 1){
            return "1";
        }
        String ret = "1";

        for(int i = 0; i < n; i++){
            //用一个StringBuilder暂存替换后的序列
            StringBuilder tmp = new StringBuilder();
            for(int left = 0, right = 0; right < ret.length();){
                while(right < ret.length() && ret.charAt(left) == ret.charAt(right)){
                    right++;
                }
                tmp.append(right - left);
                tmp.append(ret.charAt(left));
                left = right;
            }
            ret = tmp.toString();
        }
        return ret;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        String ret = solution.countAndSay(4);
    }
}
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}